<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="appKey" prop="paramKey">
        <el-input v-model="dataForm.paramKey" placeholder="appKey"></el-input>
      </el-form-item>
      <el-form-item label="appSecret" prop="paramValue">
        <el-input v-model="dataForm.paramValue" placeholder="appSecret"></el-input>
      </el-form-item>
      <el-form-item label="accessToken" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="accessToken" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="Token过期时间" prop="remark">
        <el-input v-model="dataForm.createTime" placeholder="Token过期时间" :disabled="true"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="danger" plain @click="getToken()">获取token与过期时间</el-button>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          paramKey: '',
          paramValue: '',
          remark: '',
          createTime: ''
        },
        dataRule: {
          paramKey: [
            {required: true, message: '参数名不能为空', trigger: 'blur'}
          ],
          paramValue: [
            {required: true, message: '参数值不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/config/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.paramKey = data.config.paramKey
                this.dataForm.paramValue = data.config.paramValue
                this.dataForm.remark = data.config.remark
                this.dataForm.createTime = data.config.createTime
                this.dataForm.version = data.config.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/config/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'paramKey': this.dataForm.paramKey,
                'paramValue': this.dataForm.paramValue,
                'remark': this.dataForm.remark,
                'createTime': this.dataForm.createTime,
                'version': this.dataForm.version
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getToken () {
        this.$confirm('警告！！！频繁获取Token会被拉入黑名单，请在Token快过期或过期之后获取！！！')
          .then(_ => {
            if (this.dataForm.paramKey == null || this.dataForm.paramKey == '' || this.dataForm.paramKey == undefined) {
              alert("请输入appKey！！！");
              return;
            }
            if (this.dataForm.paramValue == null || this.dataForm.paramValue == '' || this.dataForm.paramValue == undefined) {
              alert("请输入appSecret！！！");
              return;
            }
            this.$http({
              url: 'https://open.ys7.com/api/lapp/token/get',
              method: 'post',
              params: this.$http.adornParams({
                'appKey': this.dataForm.paramKey,
                'appSecret': this.dataForm.paramValue
              })
            }).then(({data}) => {
              console.log(data)
            })
          }).catch(_ => {
          return;
        });
      }
    }
  }
</script>
